package stud.mpgame.physics;
import java.util.ArrayList;

import stud.mpgame.server.map.MapData;
import stud.mpgame.utils.ICloneable;

/**
 * ...
 * @author pavel[f.pavel91@gmail.com]
 */
public class PhysicsWorld {

	private Vector2D gravity;
	
	private BroadPhase broadPhase;
	private ArrayList< Circle > circles;

    public PhysicsWorld(PhysicsWorld previous) {
        this.circles = new ArrayList< Circle >();
        this.gravity = previous.gravity;
        this.broadPhase = new BroadPhase(previous.getBroadPhase());
    }

	public PhysicsWorld(Vector2D gravity, MapData gameMap) {
		this.circles = new ArrayList< Circle >();
		
		this.gravity = gravity;
		this.broadPhase = new BroadPhase(gameMap);
	}

    public BroadPhase getBroadPhase() {
        return broadPhase;
    }

    public void addCircle(Circle circle) {
		circles.add(circle);
		broadPhase.addCircle(circle);
	}
	
	public void removeCircle(Circle circle) {
		circles.remove(circle);
		broadPhase.removeCircle(circle);
	}
	
	public void update(int deltaTime) {
		if (circles == null || circles.size() == 0)
			return;

        //System.out.println("BEFORE UPDATE" + circles.get(0).y);
		for (Circle circle : circles) {
			circle.move(circle.velocity.mul(deltaTime).addNew(gravity.mul(deltaTime * deltaTime / 2)));
			circle.velocity.add(gravity.mul(deltaTime));
		}

		broadPhase.update();
        //System.out.println("AFTER UPDATE" + circles.get(0).y);
	}
	
}